package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.PageInfo;
import com.itheima.vo.DishVoo;
import com.itheima.dto.DishFlavorDto;
import com.itheima.dto.FlavorDto;
import com.itheima.mapper.DishMapper;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.DishService;
import com.itheima.vo.DishVo;
import com.itheima.vo.PhoneDishFlavorVo;
import com.itheima.vo.PhoneDishVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author YangJiaHao
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageInfo<DishVoo> page(String name, Integer page, Integer pageSize, Integer type) {
        PageInfo<DishVoo> pageInfo = new PageInfo<>();
        Integer start = (page - 1) * pageSize;
        List<Dish> dishs = dishMapper.page(name, start, pageSize, type);
        List<DishVoo> dishVoos = new ArrayList<>();
        for (Dish dish : dishs) {
            String categoryName = dishMapper.selectCategoryNameByCategoryId(dish.getCategoryId());
            DishVoo dishVoo = new DishVoo();
            BeanUtils.copyProperties(dish, dishVoo);
            dishVoo.setCategoryName(categoryName);
            dishVoos.add(dishVoo);
        }

        pageInfo.setRecords(dishVoos);
        pageInfo.setSize(pageSize);
        pageInfo.setCurrent(page);
        pageInfo.setTotal(dishMapper.countDish(type));
        return pageInfo;
    }

    @Override
    public Boolean addDish(DishFlavorDto dishFlavorDto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishFlavorDto, dish);
        dish.setId(IdUtil.getSnowflake().nextIdStr());
        Integer result = dishMapper.insert(dish);

        List<FlavorDto> flavors = dishFlavorDto.getFlavors();
        if (!CollectionUtils.isEmpty(flavors)) {
            for (FlavorDto flavorDto : flavors) {
                if (flavorDto == null) {
                    continue;
                }

                DishFlavor dishFlavor = new DishFlavor();
                BeanUtils.copyProperties(flavorDto, dishFlavor);
                dishFlavor.setId(IdUtil.getSnowflakeNextIdStr());
                dishFlavor.setDishId(dish.getId());

                List<DishFlavor> list = new ArrayList<>();
                list.add(dishFlavor);

                // Integer res = dishMapper.insertFlavor1(dishFlavor);
                // if (res!=1){
                //     throw new RuntimeException("口味添加失败");
                // }
                Integer res = dishMapper.insertFlavor(list);
                if (res != list.size()) {
                    throw new RuntimeException("口味添加失败");
                }
            }
        }
        return result == 1;
    }

    @Override
    public Boolean modifyDish(DishFlavorDto dishFlavorDto) {
        if (dishFlavorDto == null) {
            throw new RuntimeException("参数不能为空");
        }

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishFlavorDto, dish);

        dish.setUpdateTime(LocalDateTime.now());

        Integer res = dishMapper.updateById(dish);
        if (res != 1) {
            log.error("菜品添加失败");
            return false;
        }

        Integer total = dishMapper.countFlavorByDishId(dishFlavorDto.getId());
        if (total > 0) {
            Integer removeResult = dishMapper.removeFlavorByDishId(dishFlavorDto.getId());
            if (!removeResult.equals(total)) {
                throw new RuntimeException("口味删除失败");
            }
        }
        List<FlavorDto> flavors = dishFlavorDto.getFlavors();
        List<DishFlavor> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(flavors)) {
            for (FlavorDto flavorDto : flavors) {
                if (flavorDto == null) {
                    continue;
                }
                DishFlavor dishFlavor = new DishFlavor();
                BeanUtils.copyProperties(flavorDto, dishFlavor);
                dishFlavor.setDishId(dishFlavorDto.getId());
                list.add(dishFlavor);
            }
        }
        Integer rs = dishMapper.insertFlavor(list);
        if (rs != list.size()) {
            throw new RuntimeException("口味添加失败");
        }
        return true;
    }

    @Override
    public DishVo getById(String id) {
        DishVo dishVo = new DishVo();

        Dish dish = dishMapper.selectDishById(id);
        BeanUtils.copyProperties(dish, dishVo);

        if (dish.getStatus() != 1) {
            throw new RuntimeException("查无此菜品信息");
        }

        List<DishFlavor> flavors = dishMapper.selectflavorsById(id);
        dishVo.setFlavors(flavors);

        dishVo.setCategoryName(dishMapper.selectCategoryNameByCategoryId(dish.getCategoryId()));

        return dishVo;
    }

    @Override
    public Boolean delete(String ids) {
        String[] dishIds = ids.split(",");
        Integer res = dishMapper.deleteDishById(dishIds);
        if (res>0){
            Integer result = dishMapper.deleteFlavorById(dishIds);
        }
        return res == dishIds.length;
    }

    @Override
    public Boolean status(Integer status, String ids) {
        String[] dishIds = ids.split(",");
        Integer res = dishMapper.updateStatus(status,dishIds);
        return null;
    }

    @Override
    public List<DishVo> getList(String categoryId) {
        List<Dish> dishs = dishMapper.selectDishs(categoryId);
        List<DishVo> dishVos = new ArrayList<>();
        for (Dish dish : dishs) {
            if (dish==null){
                continue;
            }
            DishVo dishVo = new DishVo();
            BeanUtils.copyProperties(dish, dishVo);

            List<DishFlavor> flavors = dishMapper.selectFlavors(dish.getId());
            dishVo.setFlavors(flavors);

            String categoryName = dishMapper.selectCategoryNameByCategoryId(categoryId);
            dishVo.setCategoryName(categoryName);

            dishVos.add(dishVo);
        }
        return dishVos;
    }

    @Override
    public List<PhoneDishVo> list(String categoryId, Integer status) {
        List<Dish> dishes = dishMapper.DishList(categoryId,status);
        List<PhoneDishVo> phoneDishVos = new ArrayList<>();
        for (Dish dish : dishes) {
            PhoneDishVo phoneDishVo = new PhoneDishVo();
            BeanUtils.copyProperties(dish, phoneDishVo);
            String categoryName = dishMapper.selectCategoryNameByCategoryId(categoryId);
            phoneDishVo.setCategoryName(categoryName);
            List<PhoneDishFlavorVo> flavors = dishMapper.selectPhoneflavorsByDishId(dish.getId());
            phoneDishVo.setFlavors(flavors);
            phoneDishVos.add(phoneDishVo);
        }
        return phoneDishVos;
    }
}